//
//  YsNoteSDKNotebookViewModel.m
//  YsNoteSDK
//
//  Created by 宋宏康 on 2020/3/31.
//

#import "YsNoteSDKNotebookViewModel.h"
#import "YsNoteSDKNetWorkManager.h"
#import "YsNoteSDKHeader.h"
#import "YsNoteSDKUserInforModel.h"
#import <YsToolsManager/NSString+YsToolsManagerQMUI.h>

@implementation YsNoteSDKNotebookViewModel
- (RACSignal *)getNoteBooks
{
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
       [YsNoteSDKNetWorkManager noteBooks:kaccountCode success:^(YsToolsManagerResultModel * _Nonnull result) {
            [subscriber sendNext:result.data];
            [subscriber sendCompleted];
        } fail:^(NSError * _Nonnull error) {
            [subscriber sendError:error];
        }];
        return nil;
    }];
}

- (RACSignal *)getLoginInSchoohWithUserAccountCode
{
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
     __block  NSURLSessionDataTask *dataTask = [YsNoteSDKNetWorkManager requestGetLoginInSchoohWithUserAccountCode:kaccountCode success:^(YsToolsManagerResultModel * _Nonnull result) {
           // 获取响应头信息
           NSHTTPURLResponse *response = (NSHTTPURLResponse *)[dataTask response];
           NSString *loginToken = [[response allHeaderFields] objectForKey:@"x-auth-token"];
             if (!loginToken) {
                 loginToken = YsNoteSDKUserDefaultsObjectForKey(@"kYsNoteSDKKeyloginToken");
             }
          // 设置到请求头
          YsNoteSDKUserDefaultsSetObjectForKey(@"kYsNoteSDKKeyloginToken", loginToken);
            [subscriber sendNext:result.data];
            [subscriber sendCompleted];
        } fail:^(NSError * _Nonnull error) {
            [subscriber sendError:error];
        }];
        return nil;
      }];
}

- (RACSignal *)userGetNoteBooks
{
    return [self.getLoginInSchoohWithUserAccountCode flattenMap:^__kindof RACSignal * _Nullable(id  _Nullable value) {
            YsNoteSDKUserInforModel.shareManager.user_account_fk_code = [[value lastObject] objectForKey:@"user_account_fk_code"];
            return self.getNoteBooks;
        }];
}

- (RACSignal *)getUserNoteBookList
{
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [YsNoteSDKNetWorkManager userNoteBooksList:YsNoteSDKUserInforModel.shareManager.user_account_fk_code book_fkcode:self.book_fkcode page_no:@"1" page_size:self.page_size note_type:self.note_type success:^(YsToolsManagerResultModel * _Nonnull result) {
            [subscriber sendNext:result.data];
            [subscriber sendCompleted];
        } fail:^(NSError * _Nonnull error) {
            [subscriber sendError:error];
        }];
        return nil;
    }];
}

- (RACSignal *)downloadCloudSpaceYsnoteZipWithUuid:(NSString *)uuid path:(NSString *)path
{
//    NSString *downloadStr = [NSString qmui_stringByConcat:YsNoteSDKCloudSpaceDownloadZip,uuid];
    NSString *downloadStr = [NSString stringWithFormat:@"%@%@",YsNoteSDKCloudSpaceDownloadZip,uuid];
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [YsNoteSDKNetWorkManager downloadURL:downloadStr path:path completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nonnull filePath, NSError * _Nonnull error) {
            if (error) {
                [subscriber sendError:error];
            }else{
                [subscriber sendNext:filePath];
                [subscriber sendCompleted];
            }
        }];
        return nil;
    }];
}
@end
